<?php

/**
 * @see Zend_Application_Bootstrap_Bootstrap
 */
require_once 'Zend/Application/Bootstrap/Bootstrap.php';

/**
 * Classe para inicializacao da aplicacao
 *
 * @category Tavs
 * @package Tavs_Application_Bootstrap
 *
 * @author Tales Santos <tales.augusto.santos>
 *
 */
class Tavs_Application_Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**
	 * Configura os autoloaders.
	 *
	 * @return void
	 */
	protected function _initLoader()
	{
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->setFallbackAutoloader(true);

		return $autoloader;
	}

	/**
	 * Retorna o objeto frontController
	 *
	 * @return Zend_Controller_Front
	 */
	protected function _getFrontController()
	{
		return Zend_Controller_Front::getInstance();
	}

	/**
	 * Configura o objeto FrontControoler.
	 *
	 * @return Zend_Controller_Front
	 */
	protected function _initFrontController()
	{
		$front = $this->_getFrontController();
		$front
			->addModuleDirectory(APPLICATION_PATH . '/modules')
			->setDefaultModule('default');

		//pasta para os helpers de Actions
		Zend_Controller_Action_HelperBroker::addPrefix('Tavs_Controller_Action_Helper_');

		return $front;
	}

	/**
	 * Inicializa o objeto de requisicao
	 *
	 * @return Zend_Controller_Request_Http
	 */
	protected function _initRequest()
	{
		$request = new Zend_Controller_Request_Http();
		$this->getResource('frontController')->setRequest($request);

		return $request;
	}
	
	/**
	 * Inicializa a conexao com o banco de dados
	 *
	 * @return null|Zend_Db_Adapter_Abstract
	 */
	protected function _initDatabase()
	{
		$options = $this->getApplication()->getOptions();
		
		$db = null;
		
		if (isset($options['database'])) {
			
			$dbOptions = (array) $options['database'];
			$db = Zend_Db::factory($dbOptions['adapter'], $dbOptions['params']);
			
			if (isset($dbOptions['isDefaultAdapter']) && (bool) $dbOptions['isDefaultAdapter']) {
				Zend_Db_Table::setDefaultAdapter($db);
			}
		}
		
		return $db;
	}

	/**
	 * Configura o objeto de renderização
	 *
	 * @return Zend_View_Interface
	 */
	protected function _initView()
	{
		$view = new Zend_View();
		$view
			->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper')
			->addHelperPath('Tavs/View/Helper', 'Tavs_View_Helper');

		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
		$viewRenderer->setView($view);
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

		//configura o layout
		Zend_Layout::startMvc(array(
			'layout' => 'default',
			'layoutPath' => APPLICATION_PATH . '/modules/default/views/layout',
			'view' => $view
		));

		return $view;
	}

	/**
	 * Inicializa os plugins
	 *
	 * @return Zend_Controller_Front
	 */
	protected function _initControllerPlugins()
	{
		$front = $this->_getFrontController();

		$options = array();

		if (property_exists($this, '_pluginViewOptions')) {
			$options = (array)$this->_pluginViewOptions;
		}

		$front->registerPlugin(new Tavs_Controller_Plugin_View($options));

		return $front;
	}

	/**
	 * Inicializa a sessao.
	 *
	 * @return void
	 */
	protected function _initSession()
	{
		$request = Zend_Controller_Front::getInstance()->getRequest();

	    if ($sessid = $request->get('PHPSESSID')) {
	        Zend_Session::setId($sessid);
	    }

	    Zend_Session::start(true);
	}

	/**
	 * Inicializa o objeto de localizacao
	 *
	 * @return Zend_Locale
	 */
	protected function _initLocale()
	{
		$locale = new Zend_Locale('auto');
		return $locale;
	}
}